package com.example.live.view

import android.annotation.SuppressLint
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.view.Gravity.BOTTOM
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.PopupWindow
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager.HORIZONTAL
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.common.entity.CommendReply
import com.example.common.mvi.BaseMviActivity
import com.example.live.R
import com.example.live.adapter.GoodsBannerAdapter
import com.example.live.adapter.GoodsCommentAdapter
import com.example.live.adapter.ShareAdapter
import com.example.live.databinding.ActivityLiveGoodsBinding
import com.example.live.entity.GoodsCommentEntity
import com.example.live.entity.GoodsEntity
import com.example.live.entity.ShareEntity
import com.umeng.socialize.ShareAction
import com.umeng.socialize.UMShareListener
import com.umeng.socialize.bean.SHARE_MEDIA
import com.umeng.socialize.media.UMImage
import com.youth.banner.indicator.CircleIndicator
import com.youth.banner.transformer.AlphaPageTransformer
import dagger.hilt.android.AndroidEntryPoint


@AndroidEntryPoint
@Route (path = "/live/LiveGoodsActivity")
class LiveGoodsActivity : BaseMviActivity<ActivityLiveGoodsBinding>() {
    override fun getLayoutId(): Int =R.layout.activity_live_goods
    @Autowired
    @JvmField
    var goods: GoodsEntity? = null

    var banner:List<String> = mutableListOf()
    var popupWindow: PopupWindow? = null
    var shareEntity:MutableList<ShareEntity> = mutableListOf(
        ShareEntity("https://ww2.sinaimg.cn/mw690/005Zk4Aqgy1hqcerik29rj30u012w76n.jpg","芝士雪豹"),
        ShareEntity("https://wx4.sinaimg.cn/mw690/006faMndly1hqns4dao43j30u0140401.jpg","虾仁不眨眼"),
        ShareEntity("https://img1.baidu.com/it/u=3254750559,1502332285&fm=253&fmt=auto&app=120&f=GIF?w=285&h=285","熊猫很困"),
        ShareEntity("https://p1-dy.bytexservice.com/img/user-avatar/d71e237251a88a91222c30a4dc3bf9e6~300x300.image" ,"鱼香 rose"),
        ShareEntity("https://img1.baidu.com/it/u=3254750559,1502332285&fm=253&fmt=auto&app=120&f=GIF?w=285&h=285","吃土少女"),
        ShareEntity("https://wx1.sinaimg.cn/mw690/005OayIHly1hqdnyvre3zj30w70w7do8.jpg" ,"摆烂小柯基"),
        ShareEntity("https://q9.itc.cn/q_70/images03/20240829/e9b4ccc3579f4787976d3458229fe1a1.jpeg","摸鱼喵星人"),
        ShareEntity("https://p1-dy.bytexservice.com/img/user-avatar/d71e237251a88a91222c30a4dc3bf9e6~300x300.image","睡不醒的猪"),
        ShareEntity("https://img1.baidu.com/it/u=3053515689,4170843785&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500","暴躁小松鼠"),
        ShareEntity("https://q9.itc.cn/q_70/images03/20240829/e9b4ccc3579f4787976d3458229fe1a1.jpeg","外卖依赖症"),
        ShareEntity("https://img0.baidu.com/it/u=306649616,2230355986&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500" ,"减肥版干饭王"),
    )
    var mockReplayComment:MutableList<GoodsCommentEntity>  = mutableListOf(
        GoodsCommentEntity(0, 0, "我来了", "2023-12-20 14:23:21", 0, "芝士雪豹", 0, 1, "https://ww2.sinaimg.cn/mw690/005Zk4Aqgy1hqcerik29rj30u012w76n.jpg" ),
        GoodsCommentEntity(0, 0, "假的真不了", "2023-12-20 14:23:23", 1, "虾仁不眨眼", 0, 1, "https://wx4.sinaimg.cn/mw690/006faMndly1hqns4dao43j30u0140401.jpg" ),
        GoodsCommentEntity(0, 0, "言之有理", "2023-12-20 14:23:25", 2, "熊猫很困", 0, 1, "https://wx1.sinaimg.cn/mw690/005OayIHly1hqdnyvre3zj30w70w7do8.jpg" ),
        GoodsCommentEntity(0, 0, "我觉得太假了，一点儿不真实！！", "2023-12-20 14:23:26", 3, "鱼香 rose", 0, 1, "https://q9.itc.cn/q_70/images03/20240829/e9b4ccc3579f4787976d3458229fe1a1.jpeg" ),
        GoodsCommentEntity(0, 0, "啥也不是！！", "2023-12-20 14:23:26", 4, "吃土少女", 0, 1, "https://img1.baidu.com/it/u=3254750559,1502332285&fm=253&fmt=auto&app=120&f=GIF?w=285&h=285" ),
        GoodsCommentEntity(0, 0, "太傻了！！", "2023-12-20 14:23:26", 5, "摆烂小柯基", 0, 1, "https://p1-dy.bytexservice.com/img/user-avatar/d71e237251a88a91222c30a4dc3bf9e6~300x300.image" ),
        GoodsCommentEntity(0, 0, "我觉得太假了，一点儿不真实！！", "2023-12-20 14:23:26", 6, "摸鱼喵星人", 0, 1, "https://p1-dy.bytexservice.com/img/user-avatar/d71e237251a88a91222c30a4dc3bf9e6~300x300.image" ),
        GoodsCommentEntity(0, 0, "给你点赞！！", "2023-12-20 14:23:26", 7, "睡不醒的猪", 0, 1, "https://p1-dy.bytexservice.com/img/user-avatar/d71e237251a88a91222c30a4dc3bf9e6~300x300.image" ),
        GoodsCommentEntity(0, 0, "努力加油！！", "2023-12-20 14:23:26", 8, "暴躁小松鼠", 0, 1, "https://img1.baidu.com/it/u=3053515689,4170843785&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500" ),
        GoodsCommentEntity(0, 0, "我觉得太假了，一点儿不真实！！", "2023-12-20 14:23:26", 9, "干饭小仓鼠", 0, 1, "https://img1.baidu.com/it/u=3053515689,4170843785&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500" ),
        GoodsCommentEntity(0, 0, "加油！！", "2023-12-20 14:23:26", 10, "减肥版干饭王", 0, 1, "https://q9.itc.cn/q_70/images03/20240829/e9b4ccc3579f4787976d3458229fe1a1.jpeg" ),
        GoodsCommentEntity(0, 0, "一点儿不真实！！", "2023-12-20 14:23:26", 11, "外卖依赖症", 0, 1, "" ),
        GoodsCommentEntity(0, 0, "牛逼牛逼，太漂亮了！！", "2023-12-20 14:23:26", 12, "气泡音小笨蛋", 0, 1, "https://img0.baidu.com/it/u=306649616,2230355986&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500" ),
    )
    lateinit var shareAdapter:ShareAdapter
    lateinit var goodsCommentAdapter: GoodsCommentAdapter
    @SuppressLint("MissingInflatedId")
    override fun initViews() {
        ARouter.getInstance().inject(this)
        goodsCommentAdapter= GoodsCommentAdapter(mockReplayComment)
        //评论列表
        mDataBinding.rv.adapter= goodsCommentAdapter
        mDataBinding.rv.layoutManager=LinearLayoutManager(this)
        //返回
        mDataBinding.ivBack.setOnClickListener {
            finish()
        }

        banner=goods!!.bannerList
        mDataBinding.goodsSolde.setText("已售"+goods!!.goods_sales_count.toString())
        mDataBinding.goodsTitle.setText(goods!!.goods_desc)
        mDataBinding.goodsNewPrice.setText("¥"+goods!!.goods_default_price.toString())
        mDataBinding.goodsOldPrice.setText("¥"+(goods!!.goods_default_price+1724).toString())
        mDataBinding.commentCount.setText(""+mockReplayComment.size+"条")

        //设置轮播图
        mDataBinding.goodsIcon.run {
            setAdapter(GoodsBannerAdapter(banner))
            setPageTransformer(AlphaPageTransformer())
            setIndicator(CircleIndicator(this@LiveGoodsActivity))
            //关联生命周期
            addBannerLifecycleObserver(this@LiveGoodsActivity)
        }
        //分享
        mDataBinding.ivShare.setOnClickListener {
            val inflater = LayoutInflater.from(this)
            val popupView = inflater.inflate(R.layout.share_dialog_layout, null)
            val width = ViewGroup.LayoutParams.MATCH_PARENT
            val height = ViewGroup.LayoutParams.WRAP_CONTENT
            popupWindow = PopupWindow(popupView, width, height).apply {
                // 设置背景，这是点击外部关闭弹窗的必要条件
                setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
                // 设置是否可点击
                isFocusable = true
                // 设置点击外部是否关闭
                isOutsideTouchable = true
                // 设置动画效果
                animationStyle = R.style.BottomSheetDialog
            }
            popupView.findViewById<TextView>(R.id.close).setOnClickListener {
                popupWindow?.dismiss()
            }
            shareAdapter= ShareAdapter(shareEntity)
            popupView.findViewById<RecyclerView>(R.id.rv1).apply {
                layoutManager = LinearLayoutManager(this@LiveGoodsActivity, HORIZONTAL, false)
                adapter = shareAdapter
            }
            shareAdapter.setOnItemClickListener { adapter, view, position ->
                ToastUtils.showLong("分享给"+shareAdapter.data[position].name+"成功")
                popupWindow?.dismiss()
            }
            popupView.findViewById<ImageView>(R.id.iv_qq).setOnClickListener {
                Share()
            }
            popupWindow?.showAtLocation(it, BOTTOM, 0, 0)
        }
    }

    private fun Share() {
        val image: UMImage = UMImage(this, goods?.goods_default_icon) //网络图片
        ShareAction(this)
            .withMedia(image)
            .setPlatform(SHARE_MEDIA.QQ)//传入平台
            .withText("hello")//分享内容
            .setCallback(object :UMShareListener{
                override fun onStart(p0: SHARE_MEDIA?) {

                }

                override fun onResult(p0: SHARE_MEDIA?) {

                }

                override fun onError(p0: SHARE_MEDIA?, p1: Throwable?) {
                    //分享失败回调
                    ToastUtils.showLong(p1?.message)
                }

                override fun onCancel(p0: SHARE_MEDIA?) {
                    ToastUtils.showLong("取消分享")
                }

            })//回调监听器
            .share();
    }
}